/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

pragma Singleton
import QtQuick 2.12
import QtQml 2.12
import org.ukui.tablet 1.0

QtObject {
    id: controller
    property bool groupPopuped: false
    property int popupedGroupId: -1
    property bool aboutToInsertGroup: false
    property var underItem: undefined
    property var aboveItem: undefined
    property int underItemId: -1
    property int newGroupId: -1
    property int groupItemCount: 16
    property var groupContainer: undefined
    property var groupContainerId: undefined

    function popupGroup(appIcon, scrollToLast) {
        if (groupPopuped) {
            return
        }        
        groupContainer = appIcon.pageContainer.pageContainer === undefined ? undefined : appIcon.pageContainer.pageContainer
        groupContainerId = appIcon.itemId
        if (appIcon.type === LauncherItem.AppIcon) {
            var groupId = modelManager.createPopupGroupModel(appIcon.itemId, aboveItem.itemId, true)
            if (groupId === -1) {
                console.warn("Create group failed!", appIcon.itemId)
                return
            }
            newGroupId = groupId
            groupContainerId = groupId
        } else {
            modelManager.popupGroup(appIcon.itemId)
            if (scrollToLast) {
                for (var pageIndex = 0; pageIndex < appIcon.pageContainer.pageContainer.count; ++pageIndex) {
                    if (modelManager.groupPageItemCount(appIcon.itemId, pageIndex) < groupItemCount) {
                        appIcon.pageContainer.pageContainer.currentIndex = pageIndex
                        break
                    }
                }
            }
        }
        groupPopuped = true
        popupedGroupId = appIcon.itemId
    }

    function minimizeGroup() {
        if (!groupPopuped) {
            return
        }
        modelManager.minimizeGroup(popupedGroupId)
        groupPopuped = false
        popupedGroupId = -1
        aboutToInsertGroup = false
    }

    function aboutToInsertItemToGroup(underItem, aboveItem) {
        modelManager.setItemState(LauncherItem.Under, underItem.itemId)
        aboutToInsertGroup = true
        controller.underItem = underItem
        underItemId = underItem.itemId
        controller.aboveItem = aboveItem
    }

    function canceInsertToGroup() {
        aboutToInsertGroup = false
    }

    //参数为当前拖拽的item的属性值
    function insertItemToGroup(itemIndex, pageIndex, aboveItemId, appIcon, appName) {
        if (groupPopuped) {
            modelManager.moveItemToGroup(itemIndex, pageIndex, controller.aboveItem.groupId, popupedGroupId)
        } else if (aboutToInsertGroup) {
            appIcon.visible = false
            appName.visible = false
            if (underItem.type !== LauncherItem.Group) {
                var groupId = modelManager.createPopupGroupModel(underItemId, aboveItemId, false)
                modelManager.setGroupState(LauncherItem.Editable, groupId)
                newGroupId = groupId
            } else {
                modelManager.moveItemToGroup(itemIndex, pageIndex, controller.aboveItem.groupId, underItemId)
                modelManager.setGroupState(LauncherItem.Editable, underItemId)
            }
            aboutToInsertGroup = false
        }
    }
}
